﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using Microsoft.Office.Interop.Excel;
using Excel_12 = Microsoft.Office.Interop.Excel;

namespace QuanLyBanHang_N3A
{
    public partial class FormThongTinKhachHang : Form
    {
        public FormThongTinKhachHang()
        {
            InitializeComponent();
        }
        void LamMoi()
        {
            txtMaKH.Text = "";
            txtTenKH.Text = "";
            txtDienThoai.Text = "";
            txtDiaChi.Text = "";
            rdNam.Checked = false;
            rdNu.Checked = false;
            cmbLoaiKH.SelectedValue = "";
            cmbLoaiKH.SelectedText = "";
            cmbLoaiKH.Text = "";
        }
        void LayDanhSach()
        {
            List<KhachHangDTO> ds = new List<KhachHangDTO>();
            KhachHangBUS bus = new KhachHangBUS();
            ds = bus.LayThongTinKH();
            int stt = 1;
            GridViewThongTinKhachHang.Rows.Clear();
            for (int i = 0; i <= ds.Count - 1; i++)
            {
                string loaikh = "KTV";
                if (ds[i].LoaiKH == 1)
                    loaikh = "TV";

                GridViewThongTinKhachHang.Rows.Add(stt, ds[i].MaKH, ds[i].TenKH, ds[i].DiaChi, ds[i].GioiTinh, ds[i].DThoai, loaikh, ds[i].TongCV);
                stt++;
            }
        }
        int KiemTraDongBo(String MaKH)
        {
            KhachHangBUS bus = new KhachHangBUS();
            List<KhachHangDTO> ds = new List<KhachHangDTO>();
            ds = bus.LayKHTheoMaKH(MaKH);
            string gtinh = " ";
            if (rdNam.Checked == true)
                gtinh = "Nam";
            else
                gtinh = "Nữ";

            if (ds[0].TenKH != txtTenKH.Text || ds[0].DiaChi != txtDiaChi.Text || ds[0].DThoai != txtDienThoai.Text||ds[0].LoaiKH!=Convert.ToInt32(cmbLoaiKH.SelectedIndex.ToString())||ds[0].GioiTinh!=gtinh)
                return 0;
            return 1;// dong bo
        }
        void LuuKhachHang()
        {
            if (txtMaKH.Text == "")
                MessageBox.Show("Chưa chọn MaKH!" + MessageBoxIcon.Error);
            else if (txtTenKH.Text == "")
                MessageBox.Show("Chưa nhập tên Khach Hàng!" + MessageBoxIcon.Error);
            else if (txtDiaChi.Text == "")
                MessageBox.Show("Chưa nhập địa chỉ!" + MessageBoxIcon.Error);
            else if (txtDienThoai.Text == "")
                MessageBox.Show(" chưa nhập số điện thoại!" + MessageBoxIcon.Error);
            else
            {
                KhachHangDTO khDTO = new KhachHangDTO();
                khDTO.MaKH = txtMaKH.Text.Trim();
                khDTO.TenKH = txtTenKH.Text.Trim();
                string gtinh = "";
                if (rdNam.Checked == true)
                    gtinh = "Nam";
                else
                    gtinh = "Nữ";
                khDTO.GioiTinh = gtinh;
                khDTO.LoaiKH = cmbLoaiKH.SelectedIndex;
                khDTO.DiaChi = txtDiaChi.Text.Trim();
                khDTO.DThoai = txtDienThoai.Text.Trim();

                KhachHangBUS khBUS = new KhachHangBUS();
                int res = khBUS.CapNhatThongTinKhachHang(khDTO);
            }
        }
        private void FormThongTinKhachHang_Load(object sender, EventArgs e)
        {
            LayDanhSach();
        }

        

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (txtMaKH.Text == "")
                this.Close();
            else
            {
                int res = KiemTraDongBo(txtMaKH.Text.Trim());
                if (res == 0)
                {
                    DialogResult col = MessageBox.Show("Bạn có muốn lưu các thay đổi", "THONG TIN KHACH HANG", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    if (DialogResult.Yes == col)
                    {
                        LuuKhachHang();
                        this.Close();
                    }
                    if (DialogResult.Cancel == col)
                        return;
                }
                this.Close();
            }
        }

        private void GridViewThongTinKhachHang_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex<0)
                return;
            txtMaKH.Text = GridViewThongTinKhachHang.Rows[e.RowIndex].Cells[1].Value.ToString();
            txtTenKH.Text = GridViewThongTinKhachHang.Rows[e.RowIndex].Cells[2].Value.ToString();
            txtDiaChi.Text = GridViewThongTinKhachHang.Rows[e.RowIndex].Cells[3].Value.ToString();
            string gioitinh = GridViewThongTinKhachHang.Rows[e.RowIndex].Cells[4].Value.ToString();
            if (gioitinh == " ")
            {
                rdNam.Checked = false;
                rdNu.Checked = false;
            }
            else
            {
                if (gioitinh == "Nam")
                    rdNam.Checked = true;
                else
                    rdNu.Checked = true;
            }
            txtDienThoai.Text = GridViewThongTinKhachHang.Rows[e.RowIndex].Cells[5].Value.ToString();
            string loaikh = GridViewThongTinKhachHang.Rows[e.RowIndex].Cells[6].Value.ToString();
            if (loaikh == "TV")
                cmbLoaiKH.SelectedIndex = 1;
            else
                cmbLoaiKH.SelectedIndex = 0;

        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            LuuKhachHang();
            LayDanhSach();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            KhachHangBUS bus = new KhachHangBUS();
            KhachHangDTO khDTO = new KhachHangDTO();
            if (MessageBox.Show("Bạn chắc chắn muốn xóa? \n Khách Hàng:" +txtMaKH.Text.Trim(), "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {   
                KhachHangBUS khBUS = new KhachHangBUS();
                int res = khBUS.XoaKH(txtMaKH.Text.Trim());
                if (res == 1)
                    MessageBox.Show("Xóa Thành Công!");
                else
                    MessageBox.Show("Xóa Thất Bại!");
                LamMoi();
                LayDanhSach();
            }     
        }

        private void btnXuatFile_Click(object sender, EventArgs e)
        {

            try
            {
                ApplicationClass excel = new ApplicationClass();
                excel.Application.Workbooks.Add(true);

                Excel_12.Application oExcel_12 = null;                //Excel_12 Application
                Excel_12.Workbook oBook = null;                       // Excel_12 Workbook
                Excel_12.Sheets oSheetsColl = null;                   // Excel_12 Worksheets collection
                Excel_12.Worksheet oSheet = null;                     // Excel_12 Worksheet
                Excel_12.Range oRange = null;                         // Cell or Range in worksheet
                Object oMissing = System.Reflection.Missing.Value;
                oExcel_12 = new Excel_12.Application();
                // Make Excel_12 visible to the user.
                oExcel_12.Visible = true;
                // Set the UserControl property so Excel_12 won't shut down.
                oExcel_12.UserControl = true;
                // System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
                // Add a workbook.
                oBook = oExcel_12.Workbooks.Add(oMissing);
                // Get worksheets collection
                oSheetsColl = oExcel_12.Worksheets;

                oSheet = (Excel_12.Worksheet)oSheetsColl.get_Item("Sheet1");

                for (int j = 0; j < GridViewThongTinKhachHang.Columns.Count; j++)
                {
                    oRange = (Excel_12.Range)oSheet.Cells[1, j + 1];
                    oRange.Value2 = GridViewThongTinKhachHang.Columns[j].HeaderText;
                }

                for (int i = 0; i < GridViewThongTinKhachHang.Rows.Count; i++)
                {
                    for (int j = 0; j < GridViewThongTinKhachHang.Columns.Count; j++)
                    {

                        oRange = (Excel_12.Range)oSheet.Cells[i + 2, j + 1];
                        oRange.Value2 = GridViewThongTinKhachHang.Rows[i].Cells[j].Value;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void txtDienThoai_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    
    }
}
